﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;

namespace QLKS
{
    public partial class frm_Setting : Form
    {
        public frm_Setting()
        {
            InitializeComponent();


        }

        private void showPass_CheckedChanged(object sender, EventArgs e)
        {
            txt_Password.PasswordChar = showPass.Checked ? '\0' : '*';
        }

        private void frm_Setting_Load(object sender, EventArgs e)
        {
            // Kiểm tra cài đặt;
            if (!File.Exists(@"Setting.ini"))
            {
                // Chua co file - tao file nhap cau hinh mac dinh;
                FileStream Settingfile = File.Create(@"Setting.ini");
                StreamWriter wr = new StreamWriter(Settingfile);
                wr.WriteLine("172.245.220.253");
                wr.WriteLine("ffeefcom_mnhote");
                wr.WriteLine("ffeefcom_mnhote");
                wr.WriteLine("hungthai");
                wr.Close();
                Settingfile.Close();
            }
            else
            {
                // Da co file - mo file doc cau hinh load len Text box;
                FileStream Settingfile = File.OpenRead(@"Setting.ini");
                StreamReader sr = new StreamReader(Settingfile);
                txt_Server.Text = sr.ReadLine();
                txt_Database.Text = sr.ReadLine();
                txt_Username.Text = sr.ReadLine();
                txt_Password.Text = sr.ReadLine();
                sr.Close();
                Settingfile.Close();
            }
            // Kiểm tra thông tin khách sạn;
            if(!File.Exists(@"Info.ini"))
            {
                // Chua co thong tin khach san;
                FileStream Infofile = File.Create(@"Info.ini");
                StreamWriter wr = new StreamWriter(Infofile);
                wr.WriteLine("Sheraton Saigon Hotel And Towers");
                wr.WriteLine("88 Đồng khởi, Quận 1, TP Hồ Chí Minh.");
                wr.WriteLine("0999 999 999");
                wr.Close();
                Infofile.Close();
            }
            else
            {
                // Đã có file;
                FileStream Infofile = File.OpenRead(@"Info.ini");
                StreamReader sr = new StreamReader(Infofile);
                txt_HTName.Text = sr.ReadLine();
                txt_adrr.Text = sr.ReadLine();
                txt_phone.Text = sr.ReadLine();
                sr.Close();
                Infofile.Close();
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if(txt_Server.Text == "" || txt_Database.Text == "" || txt_Username.Text == "" )
            {
                MessageBox.Show("Cấu hình chưa chính xác. Không thể nhập.","Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                   MySqlConnection conn = null;
                   String dataconnect = @"Server="+ txt_Server.Text +";Database="+ txt_Database.Text +";Uid="+ txt_Username.Text +";Pwd="+ txt_Password.Text +";charset=utf8;";
                   try
                   {
                       conn = new MySqlConnection(dataconnect);
                       conn.Open(); 
                       // Ghi cau hinh vao file Setting.ini
                       File.WriteAllText(@"Setting.ini", String.Empty);
                       FileStream Settingfile = File.Open(@"Setting.ini",FileMode.Open,FileAccess.Write);
                       StreamWriter wr = new StreamWriter(Settingfile);
                       wr.WriteLine(txt_Server.Text);
                       wr.WriteLine(txt_Database.Text);
                       wr.WriteLine(txt_Username.Text);
                       wr.WriteLine(txt_Password.Text);
                       wr.Close();
                       Settingfile.Close();
                       MessageBox.Show("Kết nối với Server thành công!");
                 
                   }
                   catch  
                   {
                       MessageBox.Show("Không thể kết nối đến máy chủ MySQL - Vui lòng kiểm tra thông tin!");
                      
                   }
            }
            //File.WriteAllText(@"Info.ini", String.Empty);
            FileStream Infofile = File.Open(@"Info.ini", FileMode.Open, FileAccess.Write);
            StreamWriter wrr = new StreamWriter(Infofile);
            wrr.WriteLine(txt_HTName.Text);
            wrr.WriteLine(txt_adrr.Text);
            wrr.WriteLine(txt_phone.Text);
            wrr.Close();
            Infofile.Close();
        }


    }
       
    
}
